package com.adingxiong.quickstart.project.controller;


import com.adingxiong.quickstart.project.entity.WebMenu;
import com.adingxiong.quickstart.project.entity.vo.WebMenuVo;
import com.adingxiong.quickstart.project.service.IWebMenuService;
import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 父节点id 前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-05-21
 */
@RestController
@RequestMapping("/web-menu")
@Api(tags = "菜单管理",value = "菜单管理")
public class WebMenuController {

    @Autowired
    IWebMenuService webMenuService;


    @ApiOperation(value = "根据条件查询所有的菜单")
    @RequestMapping(value = "/webs" ,method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state" ,value = "菜单状态 1.启用 2. 废弃" ,paramType = "query" ,dataType = "int" ,example = "1")
    })
    public Result<Page<WebMenuVo>>  getWebs(
           Integer state
    ){

        /**
         * 需要注意此处 sql编译的语句  下面这种写法 在控制中输出的sql为   where (parent_id = 0 and state = 0 or state = 1
         * 明显此处有问题  ,优化方案
         * 1. 嵌套or的形式
         * queryWrapper.or(t-> t.eq("state",1).eq("state",2))
         * 2. in的形式
         *queryWrapper.in("state",{1,2}) 或者 queryWrapper.in("state",1,2,3)
         */

        QueryWrapper<WebMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id" ,"0");
        if(state == null ){
            queryWrapper.eq("state" ,1).or().eq("state" , 2);
        }else {
            queryWrapper.eq("state" ,state);
        }

        List<WebMenu> parentList = webMenuService.list(queryWrapper);
        List<WebMenuVo> result = new ArrayList<>();



        List<Integer> ids = parentList.stream().map(WebMenu::getId).collect(Collectors.toList());

        HashMap<Integer,List<WebMenu>> childList = new HashMap<>();


        for(int id : ids){
            QueryWrapper<WebMenu> param = new QueryWrapper<>();
            param.eq("parent_id" ,id);
            List<WebMenu> list = webMenuService.list(param);
            childList.put(id,list);
        }


        if(parentList.size()>0){
            for(WebMenu webMenu : parentList){
                WebMenuVo webMenuVO = new WebMenuVo(webMenu);
                //属性填充  填充每个父节点的子节点数据
                List<WebMenu> webs = childList.get(webMenu.getId());
                webMenuVO.setChildList(webs);
                result.add(webMenuVO);
            }
        }

        Page<WebMenuVo> page = new Page<>();
        page.setTotal(result.size());
        page.setRecords(result);
        return  new Result().setCode(RetCode.SUCCESS.getCode()).setMsg(RetCode.SUCCESS.getMsg()).setData(page);

    }
}

